package com.tianma315.admin.product.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.tianma315.admin.common.base.CoreServiceImpl;
import com.tianma315.admin.common.exception.MessageException;
import com.tianma315.admin.product.dao.ProductDao;
import com.tianma315.admin.product.domain.Product;
import com.tianma315.admin.product.domain.vo.ProductVo;
import com.tianma315.admin.product.service.ProductService;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ProductServiceImpl extends CoreServiceImpl<ProductDao, Product> implements ProductService {
    @Override
    public boolean save(Product product) {
        Product pro = selectOne(new EntityWrapper<Product>() {{
            eq("product_name", product.getProductName());
            eq("status", Product.STATUS_ENABLE);
        }});
        if (pro != null) {
            throw new MessageException(String.format("系统已经存在 %s", product.getProductName()));
        }
        product.setStatus(Product.STATUS_ENABLE);
        return insert(product);
    }

    @Override
    public boolean edit(Product product) {
        Product p = selectById(product.getProductId());
        p.setProductName(product.getProductName());
        p.setDescription(product.getDescription());
        p.setProductSpec(product.getProductSpec());
        p.setBoxSize(product.getBoxSize());
        p.setWarranty(product.getWarranty());
        p.setProductPrice(product.getProductPrice());
        p.setIntegral(product.getIntegral());
        p.setUnit(product.getUnit());
        p.setCheckTemplateId(product.getCheckTemplateId());
        return updateById(p);
    }

    @Override
    public boolean remove(long product_id) {
        Product product = selectById(product_id);
        product.setStatus(Product.STATUS_DELETE);
        return updateById(product);
    }

    @Override
    public List<Product> getAll() {
        EntityWrapper<Product> productWrapper = new EntityWrapper<>();
        productWrapper.eq("status",1);
        return selectList(productWrapper);
    }
    @Override
    public List<Product> getAll(Product product) {
        EntityWrapper<Product> productWrapper = new EntityWrapper<>();
        productWrapper.eq("status",1);
        productWrapper.like("product_name",product.getProductName());
        return selectList(productWrapper);
    }

    @Override
    public List<Product> getAllByName(String productName) {
        Wrapper<Product> productWrapper = new EntityWrapper<>();
        productWrapper.like("product_name",productName);
        return selectList(productWrapper);
    }

    @Override
    public Page<ProductVo> getPage(int pageNumber, int pageSize, String searchKey) {
        Page<ProductVo> page = new Page<>(pageNumber, pageSize);
        page.setRecords(baseMapper.selectEnablePage(page, searchKey));
        return page;
    }

    @Override
    public Product getById(long product_id) {
        return selectById(product_id);
    }
}
